#include "ip_decoder.h"

IPDecoder::IPDecoder()
{
	pIPHead = NULL;
}

IPDecoder::IPDecoder(const u_char * packetcontent)
{
	pIPHead = (struct iphdr*)(packetcontent + sizeof(struct ether_header));
}

IPDecoder::~IPDecoder()
{
	pIPHead = NULL;
	delete pIPHead;
}

unsigned short IPDecoder::GetIPVersion()
{
	return htons(pIPHead->version);
}

unsigned int IPDecoder::GetHeadLength()
{
	return sizeof(struct iphdr);
}

unsigned int IPDecoder::GetTotalLength()
{
	return htons(pIPHead->tot_len);
}

unsigned int IPDecoder::GetIdentification()
{
	return htons(pIPHead->id);
}

unsigned short IPDecoder::GetTimeToLive()
{
	return htons(pIPHead->ttl);
}

unsigned short IPDecoder::GetProtocol()
{
	return pIPHead->protocol;
}

unsigned int IPDecoder::GetChecksum()
{
	return htons(pIPHead->check);
}

char * IPDecoder::GetDstAddr()
{
	struct in_addr addr;
	addr.s_addr = (u_long)pIPHead->daddr;
    return inet_ntoa(addr);
}

char * IPDecoder::GetSrcAddr()
{
	struct in_addr addr;
	addr.s_addr = (u_long)pIPHead->saddr;
    return inet_ntoa(addr);
}

iphdr * IPDecoder::GetIPHead()
{
	return pIPHead;
}